home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / interp / perl-5.003.tar.gz / perl-5.003.tar / perl-5.003 / lib / Net / Ping.pm
Text File  |  1996-03-25  |  2KB  |  107 lines

  1. package Net::Ping;
  2.  
  3. # Authors: karrer@bernina.ethz.ch (Andreas Karrer)
  4. #          pmarquess@bfsec.bt.co.uk (Paul Marquess)
  5.  
  6. require 5.002 ;
  7. require Exporter;
  8.  
  9. use strict ;
  10. use vars qw(@ISA @EXPORT $VERSION $tcp_proto $echo_port) ;
  11.  
  12. @ISA = qw(Exporter);
  13. @EXPORT = qw(ping pingecho);
  14. $VERSION = 1.01;
  15.  
  16. use Socket 'PF_INET', 'AF_INET', 'SOCK_STREAM';
  17. use Carp ;
  18.  
  19. $tcp_proto = (getprotobyname('tcp'))[2];
  20. $echo_port = (getservbyname('echo', 'tcp'))[2];
  21.  
  22. sub ping {
  23.     croak "ping not implemented yet. Use pingecho()";
  24. }
  25.  
  26.  
  27. sub pingecho {
  28.  
  29.     croak "usage: pingecho host [timeout]" 
  30.         unless @_ == 1 or @_ == 2 ;
  31.  
  32.     my ($host, $timeout) = @_;
  33.     my ($saddr, $ip);
  34.     my ($ret) ;
  35.     local (*PINGSOCK);
  36.  
  37.     # check if $host is alive by connecting to its echo port, within $timeout
  38.     # (default 5) seconds. returns 1 if OK, 0 if no answer, 0 if host not found
  39.  
  40.     $timeout = 5 unless $timeout;
  41.  
  42.     if ($host =~ /^\s*((\d+\.){3}\d+)\s*$/)
  43.       { $ip = pack ('C4', split (/\./, $1)) }
  44.     else
  45.       { $ip = (gethostbyname($host))[4] }
  46.  
  47.     return 0 unless $ip;        # "no such host"
  48.  
  49.     $saddr = pack('S n a4 x8', AF_INET, $echo_port, $ip);
  50.     $SIG{'ALRM'} = sub { die } ;
  51.     alarm($timeout);
  52.     
  53.     $ret = 0;
  54.     eval <<'EOM' ;
  55.     return unless socket(PINGSOCK, PF_INET, SOCK_STREAM, $tcp_proto) ;
  56.     return unless connect(PINGSOCK, $saddr) ;
  57.     $ret=1 ;
  58. EOM
  59.     alarm(0);
  60.     close(PINGSOCK);
  61.     $ret;
  62. }   
  63.  
  64. 1;
  65. __END__
  66.  
  67. =cut
  68.  
  69. =head1 NAME
  70.  
  71. Net::Ping, pingecho - check a host for upness
  72.  
  73. =head1 SYNOPSIS
  74.  
  75.     use Net::Ping;
  76.     print "'jimmy' is alive and kicking\n" if pingecho('jimmy', 10) ;
  77.  
  78. =head1 DESCRIPTION
  79.  
  80. This module contains routines to test for the reachability of remote hosts.
  81. Currently the only routine implemented is pingecho(). 
  82.  
  83. pingecho() uses a TCP echo (I<not> an ICMP one) to determine if the
  84. remote host is reachable. This is usually adequate to tell that a remote
  85. host is available to rsh(1), ftp(1), or telnet(1) onto.
  86.  
  87. =head2 Parameters
  88.  
  89. =over 5
  90.  
  91. =item hostname
  92.  
  93. The remote host to check, specified either as a hostname or as an IP address.
  94.  
  95. =item timeout
  96.  
  97. The timeout in seconds. If not specified it will default to 5 seconds.
  98.  
  99. =back
  100.  
  101. =head1 WARNING
  102.  
  103. pingecho() uses alarm to implement the timeout, so don't set another alarm
  104. while you are using it.
  105.  
  106.  
  107.